/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import pojo.DonDatHang;
import pojo.SanPham;
import service.DonDatHangService;

/**
 *
 * @author MongKieu
 */
public class GoiDonDatHangController extends AbstractController {

    private DonDatHangService donDatHangService;

    public GoiDonDatHangController() {
    }

    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView("GoiDonDatHang");
        try {
            HttpSession session = request.getSession();
            ArrayList<SanPham> gioHang = null;
            DonDatHang donDatHang = null;
            int lenghtgioHang = 0;
            String ketqua = "false";
            int coDonDatHang = 0;
            if (session.getAttribute("DonDatHang") == null) {
                donDatHang = new DonDatHang();
            }
            if (session.getAttribute("DonDatHang") != null) {
                donDatHang = (DonDatHang) session.getAttribute("DonDatHang");
                coDonDatHang = 1;
            }
            DonDatHang ddh = (DonDatHang) session.getAttribute("DonDatHang");
            if (ddh == null) {
                System.out.println("Xay ra loi");
                mv = new ModelAndView("ThongBaoLoi");
                mv.addObject("messageError", "Xin chào bạn, thông tin gởi đơn hàng không hợp lệ.");
            }
            if (coDonDatHang == 1) {
                if (session.getAttribute("GioHang") == null) {
                    gioHang = new ArrayList<SanPham>();
                    session.setAttribute("GioHang", gioHang);
                } else {
                    gioHang = (ArrayList<SanPham>) session.getAttribute("GioHang");
                    if (gioHang.size() > 0) {
                        lenghtgioHang = gioHang.size();
                        ketqua = "true";
                        donDatHangService.saveDonDatHang(donDatHang, gioHang);
                        // <editor-fold defaultstate="collapsed" desc="Tong tien pai tra">
                        double tongGiaTri = 0;
                        for (int i = 0; i < gioHang.size(); i++) {
                            SanPham sp = gioHang.get(i);
                            tongGiaTri = tongGiaTri + sp.getSoLuongDangCo() * sp.getGiaBanHienTai();
                        }
                        tongGiaTri = tongGiaTri / 20000;
                        String tien = Double.toString(tongGiaTri);
                        session.setAttribute("Payment_Amount", tien);
                        // </editor-fold>
                        session.removeAttribute("GioHang");
                        session.removeAttribute("DonDatHang");
                    }
                }
            }

            mv.addObject("listSanPham", gioHang);
            mv.addObject("DonDatHang", donDatHang);
            mv.addObject("lengt", lenghtgioHang);
            mv.addObject("ketqua", ketqua);
        } catch (Exception ex) {
            System.out.println("Lỗi: " + ex.getMessage());
            mv = new ModelAndView("ThongBaoLoi");
            mv.addObject("messageError", "Xin chào bạn, thông tin gởi đơn hàng không hợp lệ.");
        }
        return mv;
    }

    public DonDatHangService getDonDatHangService() {
        return donDatHangService;
    }

    public void setDonDatHangService(DonDatHangService donDatHangService) {
        this.donDatHangService = donDatHangService;
    }
}
